const clickBtn=document.getElementById("click")
clickBtn.addEventListener("click",()=>{
    location.href="../../index.html"
})

const urlParams = new URLSearchParams(window.location.search);
const teacherId = urlParams.get('teacher_id'); // 获取 courseId 参数
const userId = urlParams.get('user_id'); // 获取 user 参数

const teacherCardTable=document.getElementById("teacherCardTable");

async function handleTeacherInfo(){
    if (teacherId!=null){
        teacherCardTable.style.display="table";
        const caption=document.createElement("caption");
        caption.textContent="teacherId 为 "+teacherId+"的教师信息表";
        teacherCardTable.appendChild(caption);
        teacherInfo=await requestSelectTeacherInfoByTeacherId(teacherId);
        console.log(teacherInfo);
        tbody=document.createElement("tbody");
        const rows = [
            ["用户ID", teacherInfo.UserInfo.UserID],
            ["昵称", teacherInfo.UserInfo.Nickname],
            ["姓名", teacherInfo.UserInfo.Name],
            ["性别", teacherInfo.UserInfo.Gender ? "男" : "女"],
            ["签名", teacherInfo.UserInfo.Signature],
            ["是否为学生", teacherInfo.UserInfo.IsStudent ? "是" : "否"],
            ["是否为教师", teacherInfo.UserInfo.IsTeacher ? "是" : "否"],
            ["是否为管理员", teacherInfo.UserInfo.IsAdmin ? "是" : "否"],
            ["头像", `<img src="${teacherInfo.UserInfo.AvaURL}" alt="头像" width="50" height="50">`],
            ["邀请码", teacherInfo.TeacherInfo.InviteCode],
            ["自我介绍", teacherInfo.TeacherInfo.Introduction],
            ["自我介绍URL", teacherInfo.TeacherInfo.IntroductionURL]
        ];

        // 遍历数据并填充到表格行
        rows.forEach(([label, value]) => {
            const tr = document.createElement("tr");

            // 创建标签列
            const tdLabel = document.createElement("td");
            tdLabel.textContent = label;
            tr.appendChild(tdLabel);

            // 创建值列
            const tdValue = document.createElement("td");
            tdValue.innerHTML = value; // 允许html内容（如图片标签）
            tr.appendChild(tdValue);

            tbody.appendChild(tr);
        });
        const backBtnCell=document.createElement("td");
        const backBtn=document.createElement("button");
        backBtnCell.appendChild(backBtn);
        backBtn.innerText="返回到上一页"
        backBtn.addEventListener("click",()=>{
            window.history.back()
        })
        const tr=document.createElement("tr");
        tr.appendChild(backBtnCell);
        teacherCardTable.appendChild(tr)


        // 将 tbody 添加到表格中
        teacherCardTable.appendChild(tbody);
    }
}






async function requestSelectTeacherInfoByTeacherId(teacherId){
    const url=`${config.server}`+"/api/select/teacher/by/teacher/id"
    const data={
        teacher_id:parseInt(teacherId)
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        return getdata
    }else{
        console.log("获取数据失败",getdata)
    }
}

handleTeacherInfo()